package com.example.dragcontentviewdemo.helper.internal;

import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import androidx.annotation.NonNull;

import com.example.dragcontentviewdemo.helper.api.DraggableActivityGestureListener;

/**
 * Description: 连接internal包中，各类的桥梁，数据与状态量存储在这里。
 */
public class AttachInfo {

    public static final int FLAG_DIRECTION_LTR = 1;
    public static final int FLAG_ACTION_START = FLAG_DIRECTION_LTR << 1;
    public static final int FLAG_ANIMATING = FLAG_DIRECTION_LTR << 1;
    public static final int FLAG_VIEW_IS_MOVED = FLAG_ANIMATING << 1;
    public static final int FLAG_CANCEL_NEXT_UP = FLAG_VIEW_IS_MOVED << 1;
    public static final int FLAG_INTERCEPT = FLAG_CANCEL_NEXT_UP << 1;
    public static final int FLAG_VIEW_IS_HIDDEN = FLAG_INTERCEPT << 1;

    public boolean mDirection = true;
    public boolean mActionStarted = false;
    public boolean mAnimating = false;
    public boolean mViewIsMoved = false;
    public boolean mCancelNextUp = false; // 暂时分离
    public boolean mNeedPrecisionFilter = true;

    public float mFirstX = 0.f;
    public float mLastX = 0.f;

    public boolean mIntercept = true; // 手势拦截开关
    public boolean mIsViewHiddenNow = false; // 视图现在是否隐藏
    public float mXLowerBound = 0.f;
    public float mXUpperBound = 1.f;
    public MotionEvent mFirstDownEvent; // 记录第一次down事件

    public VelocityTracker mVelocityTracker = VelocityTracker.obtain();
    public DraggableActivityGestureListener callback = null;

    public float mVelocityXThreshold = 1000; //pixels per second //TODO: 魔法值要注意

    private int mFlag = FLAG_INTERCEPT | FLAG_DIRECTION_LTR;

    public AttachInfo(DraggableActivityGestureListener callback){
        this.callback = callback;
    }

    /**
     * 一次手势动作结束后需要调用
     */
    public void resetState(){
        mDirection = true;
        mActionStarted = false;
        mAnimating = false;
        mViewIsMoved = false;
        mFirstX = 0.f;
        mLastX = 0.f;
        mFirstDownEvent.recycle();
        mFirstDownEvent = null;
        mNeedPrecisionFilter = true;

        resetFlag();

    }

    public boolean hasFlag(int flag) {
        return (mFlag & flag) != 0;
    }

    public void clearFlag(int flag) {
        mFlag &= ~flag;
    }

    public void resetFlag() {

    }

    public void setFlag(int flag) {
        mFlag |= flag;
    }

}
